#include "TreeItem_Book.h"
#include "TreeItem_SubBook.h"
#include "TreeItem_Chapter.h"
#include "Common.h"
#include "DataBaseManager.h"

#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlRecord>
#include <QtCore/QVariant>

namespace Jonathan
{

   /**
    * @brief Initializes a new book tree item.
    *
    * @param dbPath Path to book's database.
    * @param parent Parent tree widget.
    */
   TreeItem_Book::TreeItem_Book (QString dbPath, QSqlDatabase db, QSqlRecord record, QTreeWidget *parent) :
      IBookListItem (parent), m_DbPath(dbPath), m_Database (db)
   {
      this->setIcon (0, Common::GetIcon (ICON_Book));
      this->m_Name = record.value ("Name").toString ();
      this->m_Type = record.value ("Type").toString ();
      this->m_Author = record.value ("Author").toString ();
      this->m_Abbreviation = record.value ("Abbreviation").toString ();
      this->m_HasSubBooks = record.value ("HasSubBooks").toBool ();

      this->setText (0, this->m_Name);
      if (this->m_Type != "Reference")
         this->addChild (new QTreeWidgetItem ());
      this->m_Database.close ();
   }

   /**
    * @brief Destructor for a book tree item.
    *
    */
   TreeItem_Book::~TreeItem_Book ()
   {
      if (this->m_Database.isOpen ())
         this->m_Database.close ();
   }

   /**
    * @brief Loads any children items for the book.
    *
    */
   void TreeItem_Book::LoadChildren ()
   {
      if (this->m_HasSubBooks)
      {
         QSqlQuery query = this->GetDatabase ().exec ("SELECT * FROM book_list ORDER BY BookOrder;");
         while (query.next ())
         {
            QSqlRecord record = query.record ();
            TreeItem_SubBook* subBookItem = new TreeItem_SubBook (record.value ("Name").toString (),
                                                                  record.value ("ChapterCount").toInt (), this);
            this->addChild (subBookItem);
         }
      }
      else
      {
         QSqlQuery query = this->GetDatabase ().exec ("SELECT COUNT(DISTINCT Chapter) AS NumChaps FROM book_text;");
         if (query.next ())
         {
            int numChapters = query.value (0).toInt ();
            for (int a = 1; a <= numChapters; a++)
               this->addChild (new TreeItem_Chapter (a, this));
         }
      }
      this->GetDatabase ().close ();
   }

   /**
    * @brief Gets the path to the databse for the book.
    *
    * @return QString Path to book's database.
    */
   QString TreeItem_Book::GetBookDbPath ()
   {
      return this->m_DbPath;
   }

   /**
    * @brief Gets the name of the book.
    *
    * @return QString Name of the book.
    */
   QString TreeItem_Book::GetName ()
   {
      return this->m_Name;
   }


   /**
    * @brief Gets the type of book.
    *
    * @return QString Type of book.
    */
   QString TreeItem_Book::GetBookType ()
   {
      return this->m_Type;
   }


   /**
    * @brief Gets the author of the book.
    *
    * @return QString Author of the book.
    */
   QString TreeItem_Book::GetBookAuthor ()
   {
      return this->m_Author;
   }


   /**
    * @brief Gets the abbreviation of the book.
    *
    * @return QString Abbreviation of the book.
    */
   QString TreeItem_Book::GetBookAbbreviation ()
   {
      return this->m_Abbreviation;
   }

   /**
    * @brief Gets if this book has sub books.
    *
    * @return bool True if there are sub books, otherwise false.
    */
   bool TreeItem_Book::HasSubBooks ()
   {
      return this->m_HasSubBooks;
   }

   /**
    * @brief Gets the database connection.
    *
    * @return QSqlDatabase Database connection.
    */
   QSqlDatabase TreeItem_Book::GetDatabase ()
   {
      if (!this->m_Database.isOpen ())
         this->m_Database.open ();
      return this->m_Database;
   }

}
